# MySQL MCP Server Go - Makefile

# 变量定义
BINARY_NAME=mysql-mcp-server
MAIN_PATH=./cmd/server
BUILD_DIR=./bin
DOCKER_IMAGE=mysql-mcp-server
DOCKER_TAG=latest

# Go相关变量
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod

# 默认目标
.PHONY: all
all: clean deps test build

# 安装依赖
.PHONY: deps
deps:
	@echo "正在安装依赖..."
	$(GOMOD) download
	$(GOMOD) tidy

# 构建
.PHONY: build
build:
	@echo "正在构建应用..."
	mkdir -p $(BUILD_DIR)
	$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PATH)

# 构建Linux版本（用于Docker）
.PHONY: build-linux
build-linux:
	@echo "正在构建Linux版本..."
	mkdir -p $(BUILD_DIR)
	GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME)-linux $(MAIN_PATH)

# 运行应用
.PHONY: run
run: build
	@echo "正在运行应用..."
	$(BUILD_DIR)/$(BINARY_NAME)

# 运行开发模式
.PHONY: dev
dev:
	@echo "正在运行开发模式..."
	$(GOCMD) run $(MAIN_PATH)

# 运行单元测试
.PHONY: test
test:
	@echo "正在运行单元测试..."
	$(GOTEST) -v ./internal/...

# 运行集成测试
.PHONY: test-integration
test-integration:
	@echo "正在运行集成测试..."
	@echo "请确保已设置测试数据库环境变量："
	@echo "  TEST_DB_HOST, TEST_DB_USER, TEST_DB_PASSWORD, TEST_DB_NAME"
	$(GOTEST) -v ./test/...

# 运行所有测试
.PHONY: test-all
test-all: test test-integration

# 运行测试并生成覆盖率报告
.PHONY: test-coverage
test-coverage:
	@echo "正在生成测试覆盖率报告..."
	$(GOTEST) -v -coverprofile=coverage.out ./internal/...
	$(GOCMD) tool cover -html=coverage.out -o coverage.html
	@echo "覆盖率报告已生成：coverage.html"

# 代码格式化
.PHONY: fmt
fmt:
	@echo "正在格式化代码..."
	$(GOCMD) fmt ./...

# 代码检查
.PHONY: vet
vet:
	@echo "正在进行代码检查..."
	$(GOCMD) vet ./...

# 代码质量检查（需要安装golangci-lint）
.PHONY: lint
lint:
	@echo "正在进行代码质量检查..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run; \
	else \
		echo "golangci-lint未安装，跳过代码质量检查"; \
		echo "安装命令：go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
	fi

# 清理
.PHONY: clean
clean:
	@echo "正在清理..."
	$(GOCLEAN)
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

# Docker相关命令

# 构建Docker镜像
.PHONY: docker-build
docker-build:
	@echo "正在构建Docker镜像..."
	docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) .

# 运行Docker容器
.PHONY: docker-run
docker-run:
	@echo "正在运行Docker容器..."
	docker run -p 8080:8080 --env-file .env $(DOCKER_IMAGE):$(DOCKER_TAG)

# 停止Docker容器
.PHONY: docker-stop
docker-stop:
	@echo "正在停止Docker容器..."
	docker stop $$(docker ps -q --filter ancestor=$(DOCKER_IMAGE):$(DOCKER_TAG))

# 清理Docker镜像
.PHONY: docker-clean
docker-clean:
	@echo "正在清理Docker镜像..."
	docker rmi $(DOCKER_IMAGE):$(DOCKER_TAG) || true

# Docker Compose相关命令

# 启动服务（包括数据库）
.PHONY: compose-up
compose-up:
	@echo "正在启动服务..."
	docker-compose up -d

# 停止服务
.PHONY: compose-down
compose-down:
	@echo "正在停止服务..."
	docker-compose down

# 查看日志
.PHONY: compose-logs
compose-logs:
	@echo "查看服务日志..."
	docker-compose logs -f

# 重启服务
.PHONY: compose-restart
compose-restart: compose-down compose-up

# 开发环境相关命令

# 初始化开发环境
.PHONY: dev-setup
dev-setup:
	@echo "正在初始化开发环境..."
	@if [ ! -f .env ]; then \
		echo "正在创建.env文件..."; \
		cp .env.example .env; \
		echo "请编辑.env文件配置数据库连接信息"; \
	fi
	@if [ ! -f config.yaml ]; then \
		echo "正在创建config.yaml文件..."; \
		cp config.yaml.example config.yaml; \
		echo "请编辑config.yaml文件配置应用设置"; \
	fi
	$(MAKE) deps

# 检查代码质量
.PHONY: check
check: fmt vet lint test

# 准备发布
.PHONY: release-prepare
release-prepare: clean check build docker-build
	@echo "发布准备完成"

# 显示帮助信息
.PHONY: help
help:
	@echo "MySQL MCP Server Go - 可用命令："
	@echo ""
	@echo "构建相关："
	@echo "  build          构建应用"
	@echo "  build-linux    构建Linux版本"
	@echo "  clean          清理构建文件"
	@echo ""
	@echo "运行相关："
	@echo "  run            运行应用"
	@echo "  dev            运行开发模式"
	@echo ""
	@echo "测试相关："
	@echo "  test           运行单元测试"
	@echo "  test-integration  运行集成测试"
	@echo "  test-all       运行所有测试"
	@echo "  test-coverage  生成测试覆盖率报告"
	@echo ""
	@echo "代码质量："
	@echo "  fmt            格式化代码"
	@echo "  vet            代码检查"
	@echo "  lint           代码质量检查"
	@echo "  check          完整代码质量检查"
	@echo ""
	@echo "Docker相关："
	@echo "  docker-build   构建Docker镜像"
	@echo "  docker-run     运行Docker容器"
	@echo "  docker-stop    停止Docker容器"
	@echo "  docker-clean   清理Docker镜像"
	@echo ""
	@echo "Docker Compose："
	@echo "  compose-up     启动服务"
	@echo "  compose-down   停止服务"
	@echo "  compose-logs   查看日志"
	@echo "  compose-restart 重启服务"
	@echo ""
	@echo "开发环境："
	@echo "  dev-setup      初始化开发环境"
	@echo "  deps           安装依赖"
	@echo ""
	@echo "其他："
	@echo "  all            执行完整构建流程"
	@echo "  release-prepare 准备发布"
	@echo "  help           显示此帮助信息"